package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.mapper.WorkOrderInfoMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.WorkOrderInfo;
import com.ruoyi.system.service.IWorkOrderInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工作单信息Controller
 * 
 * @author LK
 * @date 2024-06-22
 */
@RestController
@RequestMapping("/system/work_order_info")
public class WorkOrderInfoController extends BaseController
{
    @Autowired
    private IWorkOrderInfoService workOrderInfoService;

    /**
     * 查询工作单信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:work_order_info:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkOrderInfo workOrderInfo)
    {
        startPage();
        List<WorkOrderInfo> list = workOrderInfoService.selectWorkOrderInfoList(workOrderInfo);
        return getDataTable(list);
    }

    /**
     * 查询全部工作单信息列表,不分页
     */
    @PreAuthorize("@ss.hasPermi('system:work_order_info:list')")
    @GetMapping("/all_work_order_list")
    public TableDataInfo all_work_order_list(WorkOrderInfo workOrderInfo)
    {
//        startPage();
        List<WorkOrderInfo> list = workOrderInfoService.selectWorkOrderInfoList(workOrderInfo);
        return getDataTable(list);
    }

    /**
     * 导出工作单信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:work_order_info:export')")
    @Log(title = "工作单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkOrderInfo workOrderInfo)
    {
        List<WorkOrderInfo> list = workOrderInfoService.selectWorkOrderInfoList(workOrderInfo);
        ExcelUtil<WorkOrderInfo> util = new ExcelUtil<WorkOrderInfo>(WorkOrderInfo.class);
        util.exportExcel(response, list, "工作单信息数据");
    }

    /**
     * 获取工作单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:work_order_info:query')")
    @GetMapping(value = "/{workOrderId}")
    public AjaxResult getInfo(@PathVariable("workOrderId") String workOrderId)
    {
        return success(workOrderInfoService.selectWorkOrderInfoByWorkOrderId(workOrderId));
    }

    /**
     * 新增工作单信息
     */
    @PreAuthorize("@ss.hasPermi('system:work_order_info:add')")
    @Log(title = "工作单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkOrderInfo workOrderInfo)
    {
        WorkOrderInfo newWorkOrderInfo = new WorkOrderInfo();
        String customerId = workOrderInfo.getCustomerId();
        String workOrderId = workOrderInfo.getWorkOrderId();
        newWorkOrderInfo.setCustomerId(customerId);
        newWorkOrderInfo.setWorkOrderId(workOrderId);
        // 不允许一个用户有两个以上的工作单
        List<WorkOrderInfo> workOrderInfos = workOrderInfoService.selectWorkOrderInfoList(newWorkOrderInfo);
        if(workOrderInfos.size()>0){
            return AjaxResult.success();
        }else {
            return toAjax(workOrderInfoService.insertWorkOrderInfo(workOrderInfo));
        }
    }

    @Autowired
    WorkOrderInfoMapper workOrderInfoMapper;
    /**
     * 批量新增工作单信息
     */
    @PreAuthorize("@ss.hasPermi('system:work_order_info:add')")
    @Log(title = "工作单信息", businessType = BusinessType.INSERT)
    @PostMapping("/addWork_order_info_batch")
    public AjaxResult addWork_order_info_batch(@RequestBody List<WorkOrderInfo> workOrderInfoList)
    {


        int length = workOrderInfoList.size();

        for (int i = 0; i < length; i++) {
            WorkOrderInfo workOrderInfo = workOrderInfoList.get(i);
            String customerId = workOrderInfo.getCustomerId();
            String workOrderId = workOrderInfo.getWorkOrderId();
            WorkOrderInfo newWorkOrderInfo = new WorkOrderInfo();
            newWorkOrderInfo.setCustomerId(customerId);
            newWorkOrderInfo.setWorkOrderId(workOrderId);
            // 不允许一个用户有两个以上的工作单
            List<WorkOrderInfo> workOrderInfos = workOrderInfoService.selectWorkOrderInfoList(newWorkOrderInfo);
            if(workOrderInfos.size()>0){
                continue;
            }else {
                workOrderInfoService.insertWorkOrderInfo(workOrderInfo);
            }
        }
        return AjaxResult.success();


//        System.out.println("批量信息");
//        System.out.println(workOrderInfo);
//        return AjaxResult.success();
    }

    /**
     * 修改工作单信息
     */
    @PreAuthorize("@ss.hasPermi('system:work_order_info:edit')")
    @Log(title = "工作单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkOrderInfo workOrderInfo)
    {
        return toAjax(workOrderInfoService.updateWorkOrderInfo(workOrderInfo));
    }

    /**
     * 删除工作单信息
     */
    @PreAuthorize("@ss.hasPermi('system:work_order_info:remove')")
    @Log(title = "工作单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{workOrderIds}")
    public AjaxResult remove(@PathVariable String[] workOrderIds)
    {
        return toAjax(workOrderInfoService.deleteWorkOrderInfoByWorkOrderIds(workOrderIds));
    }
}
